const express = require('express')
const router = express.Router()
const svgCaptcha = require('svg-captcha')

const {
    userLogin,
    getUser,
    getUserName,
    getSuperUserName,
    deleteUser,
    upUser,
    lowUser,
    addUser,
} = require('../db/model/userModel/userModel')
const Mail = require('../utils/mail')

// 用户名密码登陆
/**
 * post请求
 * 参数：
 * 用户名  tu            gong    min      qiang     yan      ru     admin   lily      jack    tom   涂敏强     贡艳茹
 * 密码   123456789     123456  111111   222222   333333  444444   555555  666666   777777  888888  123456     123456
 * 验证码
 * 
 */
router.post('/login', (req, res) => {
    let {
        username,
        password,
        captcha
    } = req.body
    const result = userLogin(username, password)
    // console.log(captcha)
    // console.log(req.session)
    if (captcha === req.session.captcha) {
        result.then(data => {
            if (data.username) {
                res.send({
                    code: 200,
                    msg: '登录成功'
                })
            } else {
                res.send({
                    code: -1,
                    msg: '登录失败'
                })
            }
        })
    } else {
        res.send({
            code: -2,
            msg: '验证码不正确'
        })
    }
})

/**
 * 获取所有user
 * get请求
 * 无参数
 * 
 */
router.get('/getUser', (req, res) => {
    const result = getUser()
    return result.then(val => {
        if (val.length !== 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                userName: val
            })
        } else {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        }
    })
})


/**
 * 获取管理员
 * get请求
 * 无参数
 */
router.get('/getUserName', (req, res) => {
    const result = getUserName()
    return result.then(val => {
        if (val.length !== 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                userName: val
            })
        } else {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        }
    })
})

/**
 * 获取超级管理员
 * get请求
 * 无参数
 */
router.get('/getSuperUserName', (req, res) => {
    const result = getSuperUserName()
    return result.then(val => {
        if (val.length !== 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                userName: val
            })
        } else {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        }
    })
})

/**
 * 删除用户
 * post请求
 * 参数id
 */
router.post('/deleteUser', (req, res) => {
    const {
        idAry
    } = req.body
    const result = deleteUser(idAry)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '删除成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '删除失败'
            })
        }
    })
})

/**
 * 升级超级管理员
 * post请求
 * 参数id
 */

router.post('/upUser', (req, res) => {
    const {
        idAry
    } = req.body
    const result = upUser(idAry)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '修改成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '修改失败'
            })
        }
    })
})

/**
 * 降级管理员
 * post请求
 * 参数id
 */

router.post('/lowUser', (req, res) => {
    const {
        idAry
    } = req.body
    const result = lowUser(idAry)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '修改成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '修改失败'
            })
        }
    })
})


/**
 * 新增管理员
 * post请求
 * 参数：username，password
 */
router.post('/addUser', (req, res) => {
    const {
        username,
        password
    } = req.body
    const result = addUser(username, password)
    return result.then(val => {
        if (val) {
            res.send({
                code: 200,
                msg: '添加成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '添加失败'
            })
        }
    })
})


/*
一次性图形验证码
 */
router.get('/captcha', function (req, res) {
    var captcha = svgCaptcha.create({
        size: 6, //验证码长度
        ignoreChars: '0o1l', //验证码字符中排除'0o1l'
        noise: 2,
        color: true // 验证码的字符是否有颜色，默认没有，如果设定了背景，则默认有 
    });
    req.session.captcha = captcha.text.toLowerCase();
    // console.log(req.session)
    /*res.type('svg');
    res.status(200).send(captcha.data);*/
    res.type('svg');
    res.send(captcha.data)
})

// 发送邮箱验证码
router.post('/getMailCode', (req, res) => {
    const {
        mail
    } = req.body
    let code = parseInt(Math.random() * 10000)
    Mail.send(mail, code)
        .then(() => {
            req.session.mailCode = code
            res.send({
                code: 200,
                msg: '邮箱验证码发送成功'
            })
        })
        .catch(() => {
            res.send({
                code: -10,
                msg: '邮箱验证码发送失败'
            })
        })

})

// 邮箱验证登录
router.post('/mailLogin', (req, res) => {
    const {
        mailCode
    } = req.body
    // console.log(req.session.mailCode)
    // console.log(req.body)
    // console.log(mailCode)
    if (parseInt(mailCode) === req.session.mailCode) {
        res.send({
            code: 200,
            msg: '登录成功'
        })
    } else {
        res.send({
            code: -1,
            msg: '登录失败'
        })
    }
})

module.exports = router